//
//  InviteFriendVC.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/3/1.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "InviteFriendVC.h"
#import "AppHeader.h"
#import "MyInfoTableCell.h"
#import "ShareControlView.h"
#import "MyRecommendVC.h"

@interface InviteFriendVC () <UITableViewDelegate, UITableViewDataSource> {
    // net-邀请数量
    NSInteger inviteCount;
}

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableView_top;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation InviteFriendVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent =YES;
    self.navigationController.navigationBar.barTintColor =[UIColor clearColor];
    [self _queryInviteCount];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent =NO;
    self.navigationController.navigationBar.barTintColor =APP_DEFAULT_COLOUR;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
//    if (IS_IPHONE_X) {
//        self.tableView_top.constant =-88;
//    } else {
//        self.tableView_top.constant =-64;
//    }
//    self.tableView.contentOffset = CGPointMake(0, 0);
    
    

    
    if (@available(iOS 11.0, *)) {
                self.tableView_top.constant =-64;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        
    }
    self.tableView.rowHeight =UITableViewAutomaticDimension;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"TableViewCell_1"];

}

#pragma mark - Request Network
- (void)_queryInviteCount {
    [HttpUrl GET:@"user/selectMyInviNum" dict:nil hud:self.view isShow:NO WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            inviteCount = [data[@"data"] integerValue];
            [self.tableView reloadData];
        } else {
            if (data[@"msg"]) {
                ShowAlertTitle(data[@"msg"]);
            } else {
                ShowAlertTitle(@"获取数据错误");
            }
        }
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section ==0) {
        return 2;
    } else {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section ==0) {
        if (indexPath.row ==0) {
            if (KScreenWidth==320) {
                return 160;
            } else {
                return 170+60;
            }
        } else {
            return 260;
        }
    } else {
        return 45;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section ==0) {
        return 0.1;
    } else {
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 0.1)];
    headerView.backgroundColor =tableView.backgroundColor;
    if (section ==0) {
        headerView.frame =CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 10);
    }
    return headerView;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *footerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.1)];
    footerView.backgroundColor =tableView.backgroundColor;
    return footerView;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    if (indexPath.section ==0) {
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"TableViewCell_1" forIndexPath:indexPath];
        cell.backgroundColor =[UIColor clearColor];
        cell.selectionStyle =UITableViewCellAccessoryNone;
        if (indexPath.row ==0) {
            UIImageView *bgImageView =[cell.contentView viewWithTag:300];
            if (!bgImageView) {
                /*
                UIView *whiteView =[[UIView alloc] initWithFrame:CGRectZero];
                whiteView.backgroundColor =[UIColor whiteColor];
                whiteView.cornerRadius =8;
                [cell.contentView addSubview:whiteView];
                [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
                    // 调节白色View的高度(递增)
                    CGFloat multiplied =0.255;
                    if (IS_IPHONE_5) {
                        multiplied =0.311;
                    } else if (IS_IPHONE_6PLUS) {
                        multiplied =0.231;
                    }
                    make.height.mas_equalTo(cell.contentView.mas_width).multipliedBy(multiplied);
                    make.left.mas_equalTo(cell.contentView.mas_left).offset(15);
                    make.right.mas_equalTo(cell.contentView.mas_right).offset(-15);
                    make.bottom.mas_equalTo(cell.contentView.mas_bottom).offset(10);
                }];
                 */
                UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectZero];
                imageView.contentMode =UIViewContentModeScaleAspectFit;
                imageView.image =[UIImage imageNamed:@"wawabeijin"];
                imageView.tag =300;
                [cell.contentView addSubview:imageView];
                
                [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//                    make.top.mas_equalTo(cell.contentView.mas_top).offset(self.tableView_top.constant);
                    make.left.mas_equalTo(cell.contentView.mas_left).offset(0);
                    make.right.mas_equalTo(cell.contentView.mas_right).offset(0);
                    make.bottom.mas_equalTo(cell.contentView.mas_bottom).offset(0);
                }];
            }
        } else {
            ShareControlView *shareView =[cell.contentView viewWithTag:301];
            if (!shareView) {
                CGFloat multiplied =0.255;
                if (IS_IPHONE_5) {
                    multiplied =0.311;
                } else if (IS_IPHONE_6PLUS) {
                    multiplied =0.231;
                }
                /// 超出的高度
                CGFloat topSpace =KScreenWidth*multiplied-10;
                
                shareView =[ShareControlView loadNib:@"ShareControlView" frame:CGRectZero];
                shareView.tag =301;
                shareView.cornerRadius =8;
                shareView.title_top_space.constant =topSpace-20;
                [cell.contentView addSubview:shareView];
                [shareView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(cell.contentView.mas_top).offset(-topSpace);
                    make.left.mas_equalTo(cell.contentView.mas_left).offset(15);
                    make.right.mas_equalTo(cell.contentView.mas_right).offset(-15);
                    make.bottom.mas_equalTo(cell.contentView.mas_bottom).offset(0);
                }];
            }
            [shareView didButtonClickBlock:^(UIView *view) {
                if ([view isKindOfClass:[ILButton class]]) {
                    ILButton *button =(ILButton*)view;
                    NSLog(@"ILButton=%@", button);
                }
            }];
        }
        
        return cell;
    } else {
        MyInfoTableCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MyInfoTableCell"];
        if (!cell) {
            cell =[MyInfoTableCell findCellWithIdentifier:@"MyInfoTableCell"];
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            cell.titleLabel.textAlignment =NSTextAlignmentRight;
            cell.titleLabel.textColor =APP_DEFAULT_COLOUR;
            cell.cellBgView.cornerRadius =8;
        }
        cell.textLabel.text =@"   我的推荐";
        cell.titleLabel.text =[NSString stringWithFormat:@"%ld", inviteCount];

        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section ==1 && indexPath.row ==0) {
        MyRecommendVC *recommendVC =[[MyRecommendVC alloc] init];
        recommendVC.title =@"我的推广";
        [self.navigationController pushViewController:recommendVC animated:YES];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
